package cn.yitun.chat.yitunservermanagecore.general.manage.system.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.ParamDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.annotation.Log;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.converter.dto2po.system.MenuDto2PoConverter;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.MenuDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.MenuVo;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * date 2018-06-04 14:59:44<br>
 * description
 * 
 * @author yitun<br>
 * @since
 */
@RestController
@RequestMapping("/manage/system")
public class MenuController extends BaseController{

	@Autowired
	MenuService menuService;

	/**
	 * 菜单列表 root用户使用
	 * @return
	 */
	@PermissionMapping(name = "菜单列表", perms = "sys:menu:menu")
	@RequestMapping(method = RequestMethod.POST, value = "/menu/allList")
	public Object allList() {
		ResultMessage rm = new ResultMessage();
		List<MenuVo> list = menuService.listVo(null);
		rm.bodyPut("list", list);
		return rm;
	}

	@RequestParameter
	@PermissionMapping(name = "修改菜单可见状态", perms = "sys:menu:update")
	@RequestMapping(method = RequestMethod.POST, value = "/menu/updateVisible")
	public Object updateVisible(@ParamDefine("id") Long id,
			@ParamDefine("visible") Boolean visible) {
		ResultMessage rm = new ResultMessage();
		menuService.updateVisible(id, visible);
		return rm;
	}

	@Log("菜单新增")
	@RequestParameter
	@PermissionMapping(name = "菜单新增", perms = "sys:menu:add")
	@PostMapping("/menu/save")
	ResultMessage save(JwtUserDto jwtUserDto, @JsonDefine MenuDto menuDto) {
		setUserMsgSave(menuDto, jwtUserDto);
		if (menuService.save(menuDto) == 0)
			throw new YitunException(ExceptionEnum.ADD_FAIL);
		return new ResultMessage();
	}
	@Log("菜单编辑")
	@RequestParameter
	@PermissionMapping(name = "编辑菜单", perms = "sys:menu:update")
	@PostMapping("/menu/update")
	public Object update(@JsonDefine MenuDto menuQuery) {
		menuService.update(menuQuery);
		return new ResultMessage();
	}
	@Log("菜单删除")
	@RequestParameter
	@PermissionMapping(name = "菜单删除", perms = "sys:menu:remove")
	@DeleteMapping("/menu/remove/{menuId}")
	Object remove(@PathVariable("menuId") Long menuId) {
		menuService.removeMenu(menuId);
		return new ResultMessage();
	}

	@RequestParameter
	@PermissionMapping(name = "条件获取菜单", perms = "sys:menu:menu")
	@RequestMapping(method = RequestMethod.POST, value = "/menu/list")
	public Object list(@JsonDefine MenuDto menuQuery) {
		ResultMessage rm = new ResultMessage();
		List<MenuVo> list = menuService.listVo(menuQuery);
		rm.bodyPut("list", list);
		return rm;
	}

	@RequestParameter
	@PermissionMapping(name = "角色权限", perms = "sys:user:info")
	@RequestMapping(method = RequestMethod.POST, value = "/menu/roleMenuList")
	public Object roleMenuList(@ParamDefine("roleId") Long roleId) {
		ResultMessage rm = new ResultMessage();
		List<MenuVo> list = menuService.getRoleMenuListByRoleId(roleId);
		rm.bodyPut("list", list);
		return rm;
	}
	@RequestParameter
	@PermissionMapping(name = "用户权限", perms = "sys:user:info")
	@RequestMapping(method = RequestMethod.POST, value = "/menu/userMenuList")
	public ResultMessage userMenuList(JwtUserDto jwtUserDto) {
		ResultMessage rm = new ResultMessage();
		List<MenuVo> list = menuService.getUserMenuListByCurrentUserId(jwtUserDto.getUserId());
		rm.bodyPut("list", list);
		return rm;
	}

	/**
	 * 角色相关菜单树
	 * @return
	 */
	@RequestParameter
	@PermissionMapping(name = "角色相关菜单树", perms = "sys:menu:tree")
	@PostMapping("/menu/tree")
	public Object tree(JwtUserDto jwtUserDto) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("tree", menuService.getTree(jwtUserDto));
		return rm;
	}

	/**
	 * 表格树
	 * @return
	 */
	@RequestParameter
	@PermissionMapping(name = "角色表格树", perms = "sys:menu:tree")
	@GetMapping("/menu/treeTable")
	public Object treeTable(JwtUserDto jwtUserDto) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("treeTable", menuService.treeTable(jwtUserDto));
		return rm;
	}


	@RequestParameter
	@PermissionMapping(name = "角色权限", perms = "sys:user:info")
	@GetMapping("/menu/roleId")
	public Object menuIdsByRoleId(@ParamDefine("roleId") Long roleId){
		if (roleId == null )
			throw new YitunException(ExceptionEnum.LOSE_FILED);
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("menuList", menuService.menuIdsByRoleId(roleId));
		return rm;
	}

	@RequestParameter
	@PermissionMapping(name = "用户登录时获取的路由权限", perms = "sys:user:info")
	@GetMapping("/menu/router")
	public Object  router(JwtUserDto jwtUserDto) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("router", menuService.routerInfosByUserId(jwtUserDto));
		return rm;
	}

	/**
	 * 校验菜单名称
	 */
	@RequestParameter
	@PermissionMapping(name = "校验菜单名称", perms = "sys:menu:menu")
	@PostMapping("/checkMenuNameUnique")
	public Object checkMenuNameUnique(@JsonDefine MenuDto menuDto) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("isUnique", menuService.checkMenuNameUnique(MenuDto2PoConverter.INSTANCE.dto2do(menuDto ) ));
		return rm;
	}


}
